// 工具函数集合
import { Notify } from 'vant';


/**
 * @desc 函数防抖
 * @param func 函数
 * @param wait 延迟毫秒数
 * @param immediate true/false (是/否)即执行
 */
 export function debounce(func: () => any, wait: number, immediate?: boolean) {
  let timeout: any

  return function () {
    /* eslint-disable */
    const context = this
    const args: any = arguments
    /* eslint-enable */
    if (timeout) clearTimeout(timeout)
    if (immediate) {
      const callNow = !timeout
      timeout = setTimeout(() => {
        timeout = null
      }, wait)
      if (callNow) func.apply(context, args)
    } else {
      timeout = setTimeout(function() {
        func.apply(context, args)
      }, wait)
    }
  }
}


/**
 * 复制文字
 * @param val
 * @param sucTxt
 * @returns {boolean}
 */
export function copyText(val:string, sucTxt = '复制成功~') {
  const textarea: any = document.createElement('textarea')
  // 将该 textarea 设为 readonly 防止 iOS 下自动唤起键盘，同时将 textarea 移出可视区域
  textarea.readOnly = 'readonly'
  textarea.style.position = 'absolute'
  textarea.style.left = '-9999px'
  textarea.value = val
  document.body.appendChild(textarea)
  textarea.select()
  const res = document.execCommand('copy')
  document.body.removeChild(textarea)
  // console.log(res, 'res 测试............')
  if (res) {
    Notify({ type: 'success', message: sucTxt });
  }
  return res
}

